import { _decorator, tween, v3, assetManager, sp } from "cc";
import BasePanel from "../../core/component/BasePanel";
import Data from "../../core/manager/Data";
import WindowManager from "../../core/manager/WindowManager";
import GameConst, { BOX_TYPE } from "../../core/common/GameConst";
import Utils from "../../core/utils/Utils";
import MsgHints from "../../core/utils/MsgHints";
import { BoxLayer } from "./gameui/BoxLayer";
import AudioMgr from "../../core/utils/AudioMgr";
const { ccclass, property } = _decorator;

@ccclass("HallUI")
export class HallUI extends BasePanel {
  onBtnClicked(event: any, customEventData: any) {
    AudioMgr.Instance().playSFX("btnclick");
    var btnName = event.target.name;
    switch (btnName) {
      case "btn_setting":
        // WindowManager.ins.open("NewPackOpenedLayer");
        WindowManager.ins.open("SettingLayer");
        break;
      // 跳关
      case "btn_jumpLV":
        // WindowManager.ins.open("NewPackOpenedLayer");
        WindowManager.ins.open("JumpLVLayer");
        break;
      case "btn_play":
        WindowManager.ins.open("LevelLayer");
        break;
      case "btn_buy_life":
        if (Data.user.life >= GameConst.MAX_LIFE) {
          MsgHints.show("体力已满");
          return;
        }
        WindowManager.ins.open("BuyLifeLayer");
        break;
      case "btn_buy_coin":
        WindowManager.ins.open("BuyCoinLayer");
        break;
      case "full0":
        let info = Data.user.hasLvBox();
        WindowManager.ins.open("BoxLayer").then((com: BoxLayer) => {
          com.setInfo(
            BOX_TYPE.LEVEL,
            GameConst.getLvBoxReward(Math.floor(Data.user.lv / 10))
          );
        });
        break;
      case "full1":
        info = Data.user.hasStarBox();
        WindowManager.ins.open("BoxLayer").then((com: BoxLayer) => {
          com.setInfo(
            BOX_TYPE.STAR,
            GameConst.getStarBoxReward(Math.floor(Data.user.lv / 1000))
          );
        });
        break;
    }
  }

  start() {
    this.SetProgressBar("probar_lv", 0.1);
    let btn_play = this.GetNode("btn_play");
    tween(btn_play)
      .sequence(
        tween(btn_play).to(1, { scale: v3(1.05, 1.05, 1.05) }),
        tween(btn_play).to(1, { scale: v3(1, 1, 1) })
      )
      .repeatForever()
      .start();

    Utils.loadRes("effect_hecheng", "spine", sp.SkeletonData);
    // assetManager.getBundle('sounds').preloadDir('./')
  }

  update() {
    if (Data.user.life >= GameConst.MAX_LIFE) {
      this.SetText("lbl_life_time", "已满");
    } else {
      if (Data.user.life_cost_time == 0) {
        Data.user.life_cost_time = Date.now();
      }

      let nLeft = (Date.now() - Data.user.life_cost_time) / 1000;
      let addLife = Math.floor(nLeft / 60 / GameConst.MAX_LIFE_RECOVERY_TIME);
      if (addLife > 0) Data.user.life += addLife;
      nLeft -= addLife * GameConst.MAX_LIFE_RECOVERY_TIME * 60;
      this.SetText(
        "lbl_life_time",
        Utils.getTimeStrByS(GameConst.MAX_LIFE_RECOVERY_TIME * 60 - nLeft)
      );
    }
    this.SetText("lbl_life", Data.user.life + "");
    this.SetText("lbl_coin", Data.user.coin + "");
    this.SetText("lbl_lv", Data.user.lv + "");
    this.SetText("lbl_lv_center", Data.user.lv + "");

    let info = Data.user.hasLvBox();
    this.GetGameObject("full0").active = info;
    let lvmin = Data.user.lv % 10;
    this.SetText("lbl_pro_lv", lvmin + "/10");
    this.SetProgressBar("lbl_pro_star", lvmin / 10);
    this.SetProgressBar("probar_lv_center", lvmin / 10);
    this.SetProgressBar("probar_lv", lvmin / 10);

    info = Data.user.hasStarBox();
    if (info) {
      this.SetText("probar_star", "满");
      this.SetProgressBar("probar_star", 1);
      this.GetGameObject("full1").active = true;
    } else {
      let starmin = Data.user.star % 1000;
      this.GetGameObject("full1").active = false;
      this.SetText("lbl_pro_star", starmin + "/1000");
      this.SetProgressBar("probar_star", starmin / 1000);
    }
  }
}
